// 💩💩💩💩💩💩💩💩💩
// 修改月薪，自动计算年薪
// 修改年薪，自动计算月薪
// 修改月份，自动计算年薪
// 💩💩💩💩💩💩💩💩💩

const getChangeSalaryCfg = field => {
    const fieldGroups = [
        [
            'salarySaveDTO.suggestedYearSalary',
            'salarySaveDTO.suggestedMonthSalary',
            'salarySaveDTO.suggestedMonthNumber'
        ],
        ['salarySaveDTO.yearSalary', 'salarySaveDTO.monthSalary', 'salarySaveDTO.monthNumber'],
        ['salarySaveDTO.expectYearSalary', 'salarySaveDTO.expectMonthSalary', 'salarySaveDTO.expectMonthNumber'],
        ['suggestedYearSalary', 'suggestedMonthSalary', 'suggestedMonthNumber']
    ];
    const group = fieldGroups.find(item => item.includes(field));
    if (!group) return {};
    const [yearSalary, monthSalary, monthNumber] = group;
    const cfgMap = {
        [yearSalary]: {
            base: monthNumber,
            target: monthSalary,
            calc: (base, input) => (base ? Math.ceil(input / base) : 0)
        },
        [monthSalary]: {
            base: monthNumber,
            target: yearSalary,
            calc: (base, input) => Math.ceil(base * input)
        },
        [monthNumber]: {
            base: monthSalary,
            target: yearSalary,
            calc: (base, input) => Math.ceil(base * input)
        }
    };
    return cfgMap[field];
};

export default form => {
    const handleChangeSalary = (field, value) => {
        const { base, target, calc } = getChangeSalaryCfg(field);
        if (!calc) return;
        const baseValue = form.getFieldValue(base);
        const result = calc(baseValue, value);
        form.setFieldsValue({ [target]: Number.isNaN(result) ? null : result ?? null });
    };
    return handleChangeSalary;
};
